package net.wanho.customerManage.controller;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import net.wanho.commons.exception.ServiceException;
import net.wanho.commons.servlet.BaseServlet;
import net.wanho.commons.vo.AjaxResult;
import net.wanho.customerManage.po.LinkMan;
import net.wanho.customerManage.service.LinkManService;
import org.apache.commons.lang3.StringUtils;

@WebServlet("/LinkManServlet")
public class LinkManServlet extends BaseServlet<LinkManService> {
    /*
     * 新增
     */
    protected AjaxResult addLinkMan(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            LinkMan linkMan = super.parseRequest(request, LinkMan.class);
            super.baseService.save(linkMan);
            return AjaxResult.success();
        } catch (ServiceException e) {
            e.printStackTrace();
            return AjaxResult.fail();
        }
    }

    /*
     * 查询
     */
    protected String getAllLinkMan(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        try {
            List<LinkMan> linkManList = super.baseService.findAll();
            request.setAttribute("linkManList", linkManList);
            return "/WEB-INF/view/customerManage/contacts/index.jsp";
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }

    /*
     * 查看
     */
    protected String selectByPrimaryKey(HttpServletRequest request, HttpServletResponse response) throws ServletException {
		try {
			//type 参数 判断 跳转的目标页面  【类似 jpetstore 用户登录成功后 跳转的目标页面 to 参数值】
			//如果 type 的值为 1  跳转 查看页面   否则跳转修改页面
			String type = request.getParameter("type");
			//要查看 或 要修改的联系人 id
			String linkMainId = request.getParameter("linkMainId");
			//调用业务方法进行 查询
			LinkMan linkMan = super.baseService.findById(linkMainId);
			//设置作用域
			request.setAttribute("linkMan", linkMan);
			if ("1".equals(type)) {
				return "/WEB-INF/view/customerManage/contacts/view.jsp";
			}
			return "/WEB-INF/view/customerManage/contacts/update.jsp";
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ServletException(e.getMessage());
		}
	}

    /*
     * 修改
     */
    protected AjaxResult updateLinkMan(HttpServletRequest request, HttpServletResponse response) {
        try {
            LinkMan linkMan = super.parseRequest(request, LinkMan.class);
            super.baseService.modify(linkMan);
            return AjaxResult.success();
        } catch (ServiceException e) {
            e.printStackTrace();
            return AjaxResult.fail();
        }
    }

    /*
     * 删除
     */
    protected void deleteLinkMan(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.sendRedirect(request.getContextPath() + "/LinkManServlet?method=getAllLinkMan");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();

        }
    }

    /*
     * 搜索
     */
    protected String searchLinkMan(HttpServletRequest request, HttpServletResponse response) {
        //手动获得请求参数： 【不想污染 前端代码  请求参数名  不改】
        String mobile = request.getParameter("mobile");
        String email = request.getParameter("email");
        Map<String, String> condMap = new HashMap<>(2);
        //封装查询条件
        if (StringUtils.isNotEmpty(mobile)) {
            condMap.put("PHONENUM", mobile);
        }
        if (StringUtils.isNotEmpty(email)) {
            condMap.put("EMAIL", email);
        }
        //调用条件查询
        List<LinkMan> linkManList = super.baseService.findByCond(condMap);
        //设置作用域
        // 注意：getAllLinkMan  和 searchLinkMan 方法 跳转的目标是一致的
        //       设置值：key 必须相同  value 类型必须一致
        request.setAttribute("linkManList", linkManList);
        return "/WEB-INF/view/customerManage/contacts/index.jsp";
    }

    /*
     * 跳转到新增页面
     */
    protected String toCreateLinkManView(HttpServletRequest request, HttpServletResponse response) {
        return "/WEB-INF/view/customerManage/contacts/create.jsp";
    }

}
